﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Weight.Base;
using static Weight.Base.PhraseConfigEntity;

namespace HZH_Controls.Controls.KeyBord
{
    public partial class CarNumSelect : UserControl
    {
        /// <summary>
        /// 省份简称
        /// </summary>
        public List<Province> Porvices { get; set; }

        /// <summary>
        /// 英文字母
        /// </summary>
        public List<Letter> Letters { get; set; }

        /// <summary>
        /// 数字
        /// </summary>
        public List<string> Numbers { get; set; }

        /// <summary>
        /// 是否只显示车号输入项
        /// </summary>
        public List<int> ShowInputType { get; set; }


        /// <summary>
        /// 输入框控件
        /// </summary>
        public TextBox Txt { get; set; }

        /// <summary>
        /// 车号录入单击回调
        /// </summary>
        public Action<CommonEnum.PlateCharType, string, string> CallBack;

        /// <summary>
        /// 车号库回调函数
        /// </summary>
        public Action<DataGridView, DataGridView, DataGridView, string,string,string> DataViewGetDataCall;

        /// <summary>
        /// TabControl 选择触发事件
        /// </summary>
        public Action<string, DataGridView, string> SearchCallBack { get; set; }

        /// <summary>
        /// 设置回调
        /// </summary>
        public Action<string,DataGridView,string> SettingCallBack { get; set; }

        /// <summary>
        /// page 页映射
        /// </summary>
        public Dictionary<int, TabPage> TabPageMap = new Dictionary<int, TabPage>();

        /// <summary>
        /// 是否设置字段
        /// </summary>
        public bool IsSettingField = false;

        /// <summary>
        /// tab 标签默认选择索引
        /// </summary>
        public int DefaultSelectIndex = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="province"></param>
        /// <param name="letter"></param>
        /// <param name="callBack"></param>
        /// <param name="txt"></param>
        public CarNumSelect(List<Province> province, List<Letter> letter, Action<CommonEnum.PlateCharType, string, string> callBack, TextBox txt)
        {
            CallBack = callBack;
            Porvices = province;
            Letters = letter;
            Txt = txt;
            InitializeComponent();
        }

        private void CarNumSelect_Load(object sender, EventArgs e)
        {
            SetProvice();
            SetLetter();
            Numbers = new List<string>() { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            SetNumber();
            SetEvent(lblClear);
            SetEvent(lblDel);
            SetEvent(lblClose);
            if(!TabPageMap.Keys.Contains(1)) TabPageMap.Add(1, tabPlateTare);
            if (!TabPageMap.Keys.Contains(2)) TabPageMap.Add(2, tabPlateInput);
            if (!TabPageMap.Keys.Contains(3)) TabPageMap.Add(3, tabDispatch);
            if (!TabPageMap.Keys.Contains(4)) TabPageMap.Add(4, tabSendCard);
            SetTabPage();
            DataViewGetDataCall?.Invoke(dtView,dtViewDispatch,dtViewSendCard, string.Empty,string.Empty,string.Empty);
        }

  

        /// <summary>
        /// 初始化界面
        /// </summary>
        public void RecoveryUi()
        {
            SetTabPage();
            DataViewGetDataCall?.Invoke(dtView, dtViewDispatch, dtViewSendCard, string.Empty,string.Empty,string.Empty);
        }

        /// <summary>
        /// 设置tabPage
        /// </summary>
        private void SetTabPage()
        {
            tabControlExt1.TabPages.Clear();
            if (ShowInputType == null || ShowInputType.Count == 0)
            {
                tabControlExt1.TabPages.Add(tabPlateInput);
                tabControlExt1.SizeMode = TabSizeMode.Fixed;
                tabControlExt1.ItemSize = new Size(0, 1);
            }
            else
            {
                foreach (var item in ShowInputType)
                {
                    if (TabPageMap.Keys.Contains(item))
                    {
                        tabControlExt1.TabPages.Add(TabPageMap[item]);
                    }
                }
                tabControlExt1.SizeMode = TabSizeMode.FillToRight;
                tabControlExt1.ItemSize = new Size(0, 18);
            }

            if(TabPageMap.Keys.Contains(DefaultSelectIndex) && ShowInputType.Contains(DefaultSelectIndex))
            {
                tabControlExt1.SelectedTab = TabPageMap[DefaultSelectIndex];
            }
        }

        /// <summary>
        /// 过滤数据
        /// </summary>
        /// <param name="val"></param>
        public void Filter(string val)
        {
            DataViewGetDataCall?.Invoke(dtView, dtViewDispatch, dtViewSendCard, val,val,val);
        }


        /// <summary>
        /// 设置省份
        /// </summary>
        private void SetProvice()
        {
            if (Porvices == null) return;
            var items = Porvices.OrderByDescending(p => p.Count).ToList();
            for (int i = 0; i < fProvince.Controls.Count; i++)
            {
                var tmp = fProvince.Controls[i] as Label;
                if (i < items.Count)
                {
                    if (tmp != null)
                    {
                        tmp.Text = items[i].Name;
                        SetEvent(tmp);
                        tmp.Click += new System.EventHandler(this.Province_Click);
                    }
                }
                else
                {
                    tmp.Visible = false;
                }
            }
        }

        /// <summary>
        /// 设置字母
        /// </summary>
        private void SetLetter()
        {
            if (Letters == null) return;
            var items = Letters.OrderByDescending(p => p.Count).ToList();
            for (int i = 0; i < fLetter.Controls.Count; i++)
            {
                var tmp = fLetter.Controls[i] as Label;
                if (tmp == null) continue;
                if (i < items.Count)
                {
                    if (tmp != null)
                    {
                        tmp.Text = items[i].Name;
                        tmp.Click += new System.EventHandler(this.Letter_Click);
                        SetEvent(tmp);
                    }
                }
                else
                {
                    tmp.Visible = false;
                }
            }
        }

        /// <summary>
        /// 设置数字
        /// </summary>
        private void SetNumber()
        {
            for (int i = 0; i < fNum.Controls.Count; i++)
            {
                var tmp = fNum.Controls[i] as Label;
                if (i < Numbers.Count)
                {
                    if (tmp != null)
                    {
                        tmp.Text = Convert.ToString(Numbers[i]);
                        tmp.Click += new System.EventHandler(this.Number_Click);
                        SetEvent(tmp);
                    }
                }
                else
                {
                    tmp.Visible = false;
                }
            }
        }

        private void SetEvent(Label lab)
        {
            if (lab != null)
            {
                lab.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
                lab.MouseLeave += new System.EventHandler(this.lbl_MouseLeave);
                lab.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseUp);
            }
        }

        /// <summary>
        ///省份单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Province_Click(object sender, EventArgs e)
        {
            Label lab = sender as Label;
            string currentVal = string.Empty;
            if (lab != null)
            {
                currentVal = lab.Text;

                if (Txt != null)
                {
                    string oldVal = Txt.Text.Trim();
                    int index = FindProvincePosition(Porvices, oldVal);



                    if (index >= 0)
                    {
                        Txt.Text = oldVal.Replace(Convert.ToString(oldVal[index]), currentVal);
                    }
                    else
                    {
                        Txt.Text = currentVal + oldVal;
                    }

                }
                if (CallBack != null)
                {
                    CallBack(CommonEnum.PlateCharType.Province, currentVal, "");
                }
                SetFocus();
            }
        }

        /// <summary>
        /// 字母单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Letter_Click(object sender, EventArgs e)
        {
            Label lab = sender as Label;
            string currentVal = string.Empty;
            if (lab != null)
            {
                currentVal = lab.Text;
                if (Txt != null)
                {
                    string oldVal = Txt.Text.Trim();
                    Txt.Text = oldVal + currentVal;
                }
                if (CallBack != null)
                {
                    CallBack(CommonEnum.PlateCharType.Letter, currentVal, "");
                }
                SetFocus();
            }

        }

        /// <summary>
        /// 数字单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Number_Click(object sender, EventArgs e)
        {
            Label lab = sender as Label;
            if (lab != null)
            {
                string currentVal = lab.Text;
                if (Txt != null)
                {
                    string oldVal = Txt.Text.Trim();
                    Txt.Text = oldVal + currentVal;
                }
                if (CallBack != null)
                {
                    CallBack(CommonEnum.PlateCharType.Number, lab.Text, "");
                }
                SetFocus();
            }
        }

        /// <summary>
        /// 查找省市直辖市所在的位置
        /// </summary>
        /// <param name="items"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        private int FindProvincePosition(List<Province> items, string str)
        {
            int res = -1;
            int i = -1;
            Province tmp = null;
            foreach (var it in str.ToArray())
            {
                i++;
                tmp = items.Where(p => p.Name == it.ToString()).FirstOrDefault();
                if (tmp != null)
                {
                    res = i;
                    break;
                }
            }

            return res;
        }

        /// <summary>
        /// 查找省市直辖市所在的位置
        /// </summary>
        /// <param name="items"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        private int FindLetterPosition(List<Letter> items, string str)
        {
            int res = -1;
            int i = -1;
            Letter tmp = null;
            foreach (var it in str.ToArray())
            {
                i++;
                tmp = items.Where(p => p.Name == it.ToString()).FirstOrDefault();
                if (tmp != null)
                {
                    res = i;
                    break;
                }
            }

            return res;
        }

        private void SetFocus()
        {
            if (Txt != null)
            {
                //让文本框获取焦点 
                this.Txt.Focus();
                //设置光标的位置到文本尾 
                Txt.Select(Txt.TextLength, 0);
                //滚动到控件光标处 
                Txt.ScrollToCaret();
            }
        }


        private void lblClose_Click(object sender, EventArgs e)
        {
            this.Parent.Hide();
        }

        private void lblClear_Click(object sender, EventArgs e)
        {
            if (Txt != null)
            {
                Txt.Text = string.Empty;
            }
        }

        private void lblDel_Click(object sender, EventArgs e)
        {
            if (Txt != null)
            {
                string oldVal = Txt.Text.Trim();
                if (oldVal.Length > 0)
                {
                    Txt.Text = oldVal.Substring(0, oldVal.Length - 1);
                    SetFocus();
                }

            }
        }

        private void lblDel_MouseHover(object sender, EventArgs e)
        {
            // lostActive(sender);
        }

        private void lbl_MouseLeave(object sender, EventArgs e)
        {
            lostActive(sender);
        }

        private void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            ActiveState(sender);
        }

        private void lbl_MouseUp(object sender, MouseEventArgs e)
        {
            lostActive(sender);
        }

        private void ActiveState(object sender)
        {
            Label ctr = sender as Label;
            if (ctr != null)
            {
                ctr.BackColor = Constant.UiColor;
                ctr.ForeColor = Color.White;
            }
        }

        private void lostActive(object sender)
        {
            Label ctr = sender as Label;
            if (ctr != null)
            {
                ctr.BackColor = Color.White;
                ctr.ForeColor = Color.FromArgb(102, 102, 102);
            }
        }

        private void dtView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void txtSearch_MouseClick(object sender, MouseEventArgs e)
        {
            this.ActiveControl = txtSearchDispatch;
            this.txtSearchDispatch.Focus();
        }

        /// <summary>
        /// 选择tab 触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControlExt1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        #region 检索派车

        private void btnDispatchSearch_BtnClick(object sender, EventArgs e)
        {
            string filterVal = txtSearchDispatch.Text.Trim();
            SearchCallBack("dispatch", dtViewDispatch, filterVal);
        }

        private void txtSearchDispatch_TextChanged(object sender, EventArgs e)
        {
            string filterVal = txtSearchDispatch.Text.Trim();
            SearchCallBack("dispatch", dtViewDispatch, filterVal);
        }

        #endregion 检索派车

        #region 检索发卡
        private void btnSearchSendCard_BtnClick(object sender, EventArgs e)
        {
            string filterVal = txtSearchSendCard.Text.Trim(); 
            SearchCallBack("sendCard", dtViewSendCard, filterVal); 
        }

        private void txtSearchSendCard_TextChanged(object sender, EventArgs e)
        {
            string filterVal = txtSearchSendCard.Text.Trim();
            SearchCallBack("sendCard", dtViewSendCard, filterVal);
        }

        #endregion 检索发卡

        /// <summary>
        /// 派车设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDispatchFieldSetting_BtnClick(object sender, EventArgs e)
        {
            string filterVal = txtSearchDispatch.Text.Trim();
            if(SettingCallBack != null)
            {
                IsSettingField = true;
                SettingCallBack("dispatch", dtViewDispatch, filterVal);
                IsSettingField = false;
            }
        }

        /// <summary>
        /// 发卡设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendCardSetting_BtnClick(object sender, EventArgs e)
        {
            string filterVal = txtSearchSendCard.Text.Trim();
            if (SettingCallBack != null)
            {
                IsSettingField = true;
                SettingCallBack("sendCardSelect", dtViewSendCard, filterVal);
                IsSettingField = false;
            }
        }
    }
}
